<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="订单号码" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单流水" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单流水"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable size="small">
          <el-option
            v-for="dict in orderStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付方式" clearable size="small">
          <el-option
            v-for="dict in payTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入下单手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="dict in orderTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="请选择支付状态" clearable size="small">
          <el-option
            v-for="dict in payStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发货状态" prop="expressStatus">
        <el-select v-model="queryParams.expressStatus" placeholder="请选择发货状态" clearable size="small">
          <el-option
            v-for="dict in expressStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="退货状态" prop="refundStatus">
        <el-select v-model="queryParams.refundStatus" placeholder="请选择退货状态" clearable size="small">
          <el-option
            v-for="dict in refundStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['content:order:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="viewDetail"
          v-hasPermi="['content:order:edit']"
        >详情</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['content:order:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['content:order:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="退货状态" align="center" prop="id" />
      <el-table-column label="订单号码" align="center" prop="orderNo" width="220" />
      <el-table-column label="订单流水" align="center" prop="orderSn" width="240" />
      <el-table-column label="订单金额" align="center" prop="orderPrice" />
      <el-table-column label="订单实际金额" align="center" prop="orderRealPrice" />
      <el-table-column label="顶大优惠金额" align="center" prop="orderCouponPrice" />
      <!-- <el-table-column label="订单优惠id" align="center" prop="orderCouponId" /> -->
      <!-- <el-table-column label="用户id" align="center" prop="userId" /> -->
      <el-table-column
        label="订单状态"
        align="center"
        prop="orderStatus"
        :formatter="orderStatusFormat"
      />
      <el-table-column label="支付方式" align="center" prop="payType" :formatter="payTypeFormat" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下单手机号" align="center" prop="phone" />
      <!-- <el-table-column label="配送地址id" align="center" prop="deliveryId" /> -->
      <el-table-column label="类型" align="center" prop="orderType" :formatter="orderTypeFormat" />
      <el-table-column label="支付状态" align="center" prop="payStatus" :formatter="payStatusFormat" />
      <el-table-column
        label="发货状态"
        align="center"
        prop="expressStatus"
        :formatter="expressStatusFormat"
      />
      <el-table-column
        label="退货状态"
        align="center"
        prop="refundStatus"
        :formatter="refundStatusFormat"
      />
      <el-table-column label="下单用户" align="center" prop="userName" />
      <!-- <el-table-column label="最后一次加的价格" align="center" prop="lastAddPrice" /> -->
      <el-table-column label="操作" align="center" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="viewDetail(scope.row)"
            v-hasPermi="['content:order:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="startExpress(scope.row)"
            v-hasPermi="['content:order:remove']"
          >发货</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['content:order:remove']"
          >退款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改顶大对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
        
        <div>
          <div  class="item"  style="display:inline-block;width:70%">订单号：{{orderDetail.orderNo}}</div>
        <div class="item" style="display:inline-block">总额：<div style="color:red;display:inline-block">{{orderDetail.amt}}</div> 元</div>
        </div>
        <div  class="item">创建订单时间：{{orderDetail.createTime}}</div>
        <div  class="item">支付状态：{{orderDetail.payStatus}}</div>
        <div  class="item">下单用户：{{orderDetail.userName}}</div>
        <div  class="item">下单手机号：{{orderDetail.phone}}</div>
        <div  class="item">快递信息：{{orderDetail.expressInfo==null?"暂无快递信息":orderDetail.expressInfo}}</div>
      <el-table
        v-loading="loading"
        :data="orderDetail.goodsList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="商品名称" align="center" prop="goodsName" />
        <el-table-column label="商品类型" align="center" prop="goodsType" />
            <el-table-column label="单价" align="center" prop="price" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改顶大对话框 -->
    <el-dialog :title="title" :visible.sync="open1" width="700px" append-to-body>
       <div v-if="expressEntity.isEdit==0">
              <el-form ref="form" label-width="80px">
        <el-form-item label="订单号：">
          <el-input v-model="expressEntity.orderNo" placeholder="订单号" hidden></el-input>
        </el-form-item>
        <el-form-item label="选择快递公司">
          <el-select
            v-model="expressEntity.expressCompany"
            placeholder="选择快递公司"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in epressOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="快递订单号码：">
          <el-input v-model="expressEntity.expressNo" placeholder="输入快递单号"></el-input>
        </el-form-item>
      

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="express">发货</el-button>
        <!-- <el-button @click="cancel">取 消</el-button> -->
      </div>
       </div>
      <div v-if="expressEntity.isEdit==1">
     
           <el-form ref="form" label-width="80px">
               <div style="font-size: 20px;"  v-for-key="(item,i) in epressOptions" v-for="(item, i) in epressOptions" :key="item.id">
                  <div style="font-size: 20px;" v-if="item.dictValue==expressEntity.expressCompany">快递公司：{{item.dictLabel}}</div>
               </div>
               <div style="font-size: 20px;margin-bottom: 20px;">快递单号：{{expressEntity.expressNo}}</div>
                  <el-form-item label="快递详情">
        <el-timeline :reverse="true">
          <el-timeline-item
            v-for="(activity, index) in expressEntity.expressList"
            :key="index"
            :timestamp="activity.timestamp"
          >{{activity.content}}</el-timeline-item>
        </el-timeline>
          </el-form-item>
        </el-form>

      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
  exportOrder,
  orderDetailList,
  getOrderExpress,
  insertOrderExpress
} from "@/api/content/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 顶大表格数据
      orderList: [],
      //查看详细订单信息
      orderDetailList: [],
       orderDetail:"",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      open1: false,
      // 订单状态100完成，200发货，300收货，400退款，500待发货600未发货字典
      orderStatusOptions: [],
      // 支付方式字典
      payTypeOptions: [],
      //快递公司选择
      epressOptions: [],
      // 类型vitual,real  VIRTUAL,REAL字典
      orderTypeOptions: [],
      // 支付状态1支付0是未支付2是退款字典
      payStatusOptions: [],
      // 发货状态字典
      expressStatusOptions: [],
      // 退货状态字典
      refundStatusOptions: [],
      //快递信息
      expressEntity: {
        orderNo: undefined,
        expressNo: undefined,
        expressCompany: undefined
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: undefined,
        orderSn: undefined,
        orderPrice: undefined,
        orderRealPrice: undefined,
        orderCouponPrice: undefined,
        orderCouponId: undefined,
        userId: undefined,
        orderStatus: undefined,
        payType: undefined,
        payTime: undefined,
        phone: undefined,
        deliveryId: undefined,
        orderType: undefined,
        payStatus: undefined,
        expressStatus: undefined,
        refundStatus: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // activities: [
      //   {
      //     content: "上海分拨中心/装件入车扫描",
      //     timestamp: "2012-08-28 16:33:19"
      //   },
      //   {
      //     content: "上海分拨中心/下车扫描",
      //     timestamp: "012-08-27 23:22:42"
      //   }
      // ]
    };
  },
  created() {
    this.getList();
    this.getDicts("plat_order_status").then(response => {
      this.orderStatusOptions = response.data;
    });
    this.getDicts("plat_pay_way").then(response => {
      this.payTypeOptions = response.data;
    });
    this.getDicts("plat_order_type").then(response => {
      this.orderTypeOptions = response.data;
    });
    this.getDicts("plat_pay_status").then(response => {
      this.payStatusOptions = response.data;
    });
    this.getDicts("plat_express_status").then(response => {
      this.expressStatusOptions = response.data;
    });
    this.getDicts("plat_refund_status").then(response => {
      this.refundStatusOptions = response.data;
    });
    this.getDicts("plat_express_company").then(response => {
      this.epressOptions = response.data;
    });
  },
  methods: {
    /** 查询顶大列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 订单状态100完成，200发货，300收货，400退款，500待发货600未发货字典翻译
    orderStatusFormat(row, column) {
      return this.selectDictLabel(this.orderStatusOptions, row.orderStatus);
    },
    // 支付方式字典翻译
    payTypeFormat(row, column) {
      return this.selectDictLabel(this.payTypeOptions, row.payType);
    },
    // 类型vitual,real  VIRTUAL,REAL字典翻译
    orderTypeFormat(row, column) {
      return this.selectDictLabel(this.orderTypeOptions, row.orderType);
    },
    // 支付状态1支付0是未支付2是退款字典翻译
    payStatusFormat(row, column) {
      return this.selectDictLabel(this.payStatusOptions, row.payStatus);
    },
    // 发货状态字典翻译
    expressStatusFormat(row, column) {
      return this.selectDictLabel(this.expressStatusOptions, row.expressStatus);
    },
    // 退货状态字典翻译
    refundStatusFormat(row, column) {
      return this.selectDictLabel(this.refundStatusOptions, row.refundStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        orderNo: undefined,
        orderSn: undefined,
        createTime: undefined,
        orderPrice: undefined,
        orderRealPrice: undefined,
        orderCouponPrice: undefined,
        orderCouponId: undefined,
        userId: undefined,
        orderStatus: "0",
        payType: "0",
        payTime: undefined,
        phone: undefined,
        deliveryId: undefined,
        orderType: "0",
        payStatus: "0",
        expressStatus: "0",
        refundStatus: "0",
        userName: undefined,
        lastAddPrice: undefined
      };
      (this.expressEntity = {
        orderNo: undefined,
        expressNo: undefined,
        expressCompany: undefined
      }),
        (this.orderDetailList = null);
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加顶大";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改顶大";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除顶大编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delOrder(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    viewDetail(row) {
      console.log(row);
      this.reset();
      var r = row;
      orderDetailList(r).then(response => {
        console.log(response);
        if (response.code === 200) {
          // this.msgSuccess("修改成功");
          // this.open = false;
          // this.getList();
          this.open = true;
          // this.orderDetailList = response.data;
          this.orderDetail =  response.data;
        } else {
          this.msgError(response.msg);
        }
      });
    },
    startExpress(row) {
      console.log(row);
      this.reset();
      if (row.orderType == "YSP") {
        this.open1 = true;
        this.expressEntity.orderNo = row.orderNo;
        var r = row;
        getOrderExpress(r).then(response => {
          console.log(response);
          if (response.code === 200) {
            // this.msgSuccess("修改成功");
            // this.open = false;
            // this.getList();
            if (response.data != null) {
              this.expressEntity = response.data;
               //this.msgSuccess(response.msg);
            }
          } else {
            this.msgError(response.msg);
          }
        });
      } else {
        this.msgError("虚拟商品不支持发货");
      }
    },
    express() {
      console.log("新增发货");
      insertOrderExpress(this.expressEntity).then(response => {
        console.log(response);
        if (response.code === 200) {
          this.expressEntity = response.data;
        } else {
          this.msgError(response.msg);
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有顶大数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportOrder(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.item{
   font-size: 20px;
    margin: 15px;
}
</style>